import {useTableActionRedux} from "@/components/ant-ext-table/redux/HAdminContext";
import {useActionForm,useEdit} from "@/common/hooks";
import HModal from "@/components/ant-ext/HModal";
import {Form} from "antd";
import HFormItem from "@/components/ant-ext/HFormItem";
import CoopBank,{SyBankEntity} from "@/api/bank-list";
import {useState} from "react";

export default function AdminEdit() {
    const [loading, setLoading] = useState(false)
    const {action, reset} = useTableActionRedux<SyBankEntity>()
    const {form, key, title} = useActionForm(action, [CoopBank.EDIT], CoopBank.MAPPING)
    const close = () => {
        reset()
        form.resetFields()
    }
    const saveAndFlush = useEdit(key===CoopBank.EDIT ? CoopBank.doEdit : CoopBank.doSave, {
        setLoading: setLoading,
        success: () => {
            reset()
        }
      })

    return <>
        <HModal onCancel={close} type={"setUp"} width={500} onOk={form.submit}
                confirmLoading={loading}
                visible={title !== undefined}
                title={title}
        >
            <Form name={"editForm"} form={form} labelCol={{span: 5}}
                  onFinish={() => {
                    saveAndFlush({ body: {...form.getFieldsValue()} })
                    
                  }}>
                <HFormItem label={"id"} name={"id"} hidden={key === CoopBank.ADD}
                           disabled={key !== CoopBank.ADD}/>
                <HFormItem label={"银行名称"} name={"bankName"} required/>
                <HFormItem label={"联系人"} name={"contacts"} required/>
                <HFormItem label={"联系方式"} name={"contactsPhone"} required/>

            </Form>
        </HModal>
    </>
}